import 'dart:io';

import 'package:flutter/material.dart';

import 'package:flutter_note/note/widgets.dart';
import 'package:flutter_note/note/base.dart';
import 'package:flutter_note/note/bean.dart';
import 'package:flutter_note/note/const.dart';
import 'package:flutter_note/note/manager.dart';
import 'package:flutter_note/note/utils.dart';

class NoteAddPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _NoteAddPageState();
  }
}

class _NoteAddPageState extends BaseState<NoteAddPage> {
  TextEditingController _msgController = TextEditingController();
  File _image;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldkey,
      backgroundColor: color.bg_page,
      appBar: AppBar(
        title: Text('写日记'),
        elevation: dim.x2,
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              _createNote();
            },
            child: Container(
                color: Colors.transparent,
                width: dim.x60,
                child: Icon(Icons.check)),
          )
        ],
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: _msgTextField(),
          ),
          Row(
            children: <Widget>[
              Space(Const.SPACE_NULL),
              _picture(),
            ],
          ),
        ],
      ),
    );
  }

  Widget _msgTextField() {
    return Container(
      padding: EdgeInsets.only(left: dim.x15, right: dim.x15),
      color: color.white,
      child: TextField(
        maxLines: null,
        controller: _msgController,
        keyboardType: TextInputType.text,
        style: TextStyle(fontSize: dim.t16, color: color.txt3),
        decoration: InputDecoration(
            border: InputBorder.none,
            hintText: '开始写日记',
            hintStyle: TextStyle(fontSize: dim.t16, color: color.txt9)),
      ),
    );
  }

  Widget _picture() {
    return GestureDetector(
      onTap: () {
        _pickAndUpload();
      },
      child: _headImage(),
    );
  }

  Widget _headImage() {
    var size = dim.x50;
    if (_image != null) {
      return Center(
        child: ClipOval(
          child: Image.file(_image, width: size, height: size),
        ),
      );
    } else {
      return Center(
        child: ClipImageView(null, size, size, Const.IMG_SIZE_NOTE),
      );
    }
  }

  //选择图片,并且上传
  _pickAndUpload() async {
    File image = await ImageUtils.pickImage(true); //上传头像,需要剪裁
    if (image == null) {
      showMessage('选择图片失败');
      return;
    }
    Reply reply = await UploadFileManager.getInstance().uploadFile(image);
    setState(() {
      if (reply.code == Const.SUCCESS) {
        _image = image; //上传成功就更新图片
      } else {
        showMessage('上传图片失败');
      }
    });
  }

  //发送创建笔记本
  _createNote() async {
    if (EmptyUtils.isEmpty(_msgController.text)) {
      showMessage('内容不能为空');
      return;
    }
    show();
    String head = ''; //默认头像
    if (_image != null) {
      head = _image.path.substring(_image.path.lastIndexOf('/') + 1); //头像的名字
    }
    Reply reply = await NoteManager.getInstance().createNote(
        BookNoteManager.getInstance().getCurrentBook().bid,
        _msgController.text,
        head);
    setState(() {
      dismiss();
      if (reply.code == Const.SUCCESS) {
        Navigator.of(context).pop(reply);
      } else {
        showMessage('新建日记失败');
      }
    });
  }
}
